package com.hbzhit.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 * @author DING
 * @date 2021/4/7 14:59
 * @version 1.0
 **/
@ApiModel("分页数据")
@Getter
@Setter
public class PageUtils<T> implements Serializable {

	@ApiModelProperty("总记录数")
	private int totalCount;

	@ApiModelProperty("每页记录数")
	private int pageSize;

	@ApiModelProperty("总页数")
	private int totalPage;

	@ApiModelProperty("当前页数")
	private int currPage;

	@ApiModelProperty("列表数据")
	private List<T> list;

	@ApiModelProperty("合计行")
	private T totalSum;


	/**
	 * 根据 totalCount， pageSize， currPage 获取 start end
	 */
	public static Long[] getStartEnd(Long page, Long size, Long count) {
		long start = 0L;
		Long end = size;
		if (page > 1) {
			long inter = page * size;
			start += inter;
			end += inter;
			if (end >= count) {
				end = count;
			}
		}
		return new Long[]{ start, end };
	}

	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalCount  总记录数
	 * @param pageSize    每页记录数
	 * @param currPage    当前页数
	 */
	public PageUtils(List<T> list, int totalCount, int pageSize, int currPage) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
	}

	/**
	 * 分页
	 */
	public PageUtils(IPage<T> page) {
		this.list = page.getRecords();
		this.totalCount = (int)page.getTotal();
		this.pageSize = (int)page.getSize();
		this.currPage = (int)page.getCurrent();
		this.totalPage = (int)page.getPages();
	}


}
